// pages/shopping/order.js
import {
    store,
    user,
    marketing
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: '',
        address: '',
        note: '',
        mergedArr: '',
        sum: '',
        delivery_price: '',
        showCoupon: false,
        listCou: [],
        radio: '',
        couItem:"",
        address_id:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.obj) {
            let obj = JSON.parse(options.obj)
            console.log(obj)

            this.setData({
                info: obj
            })
            this.toConOrder()
        }
        if (options.mergedArr) {
            let mergedArr = JSON.parse(options.mergedArr)
            console.log(mergedArr)
            let arr = []
            let sum = 0
            mergedArr.forEach(item => {
                item.list.forEach(item2 => {
                    arr.push(item2.id)
                    sum += Number(item2.price) * Number(item2.quantity)
                })
            })
            console.log(sum)
            this.setData({
                mergedArr: mergedArr,
                cart_ids: arr.toString(),
                sum
            })
            this.toConOrder()
        }

        this.getList()

    },
    toConOrder(){
        if(this.data.info!=''){
            store.order.confirm({
                mode: 1,
                product_id: this.data.info.product_id,
                product_sku_id: this.data.info.product_sku_id,
                quantity: this.data.info.quantity,
                address_id:this.data.address_id,
                user_coupon_id:this.data.coupon_id
            }, res => {
                if (res.code == 1) {
                    this.setData({
                        delivery_price: res.data.delivery_price,
                        sum: res.data.pay_price
                    })
                    console.log(res.data.delivery_price)
                }
            })
        }
        if(this.data.mergedArr!=''){
            store.order.confirm({
                mode: 2,
                cart_ids: this.data.cart_ids,
                address_id:this.data.address_id,
                user_coupon_id:this.data.coupon_id
            }, res => {
                if (res.code == 1) {
                    this.setData({
                        delivery_price: res.data.delivery_price,
                        sum: res.data.pay_price
                    })
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            })
        }

    
    },
    onClick(event) {
        console.log(event)
        const {
            id,
            item
        } = event.currentTarget.dataset;
        this.setData({
            radio: id,
            coupon_id:id,
            couItem:item
        });
        this.toConOrder()
    },
    toCancelCou(){
        this.setData({
            radio:'',
            coupon_id:'',
            couItem:'',
            showCoupon: false
        })
        this.toConOrder()
    },
    toShowCou() {
        this.setData({
            showCoupon: true
        })
    },
    onCloseCou() {
        this.setData({
            showCoupon: false
        })
    },
    getList() {
        user.coupon.usableList({
            use_scene: 3,
            mode:1,
            product_sku_id:this.data.info.product_sku_id,
            quantity:this.data.info.quantity,
            cart_ids:this.data.cart_ids,
        }, res => {
            if (res.code == 1) {
                this.setData({
                    listCou: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    onChangeCou(e){
        // this.setData({
        //     radio: e.detail,
        // });
    },
    toComCou(){
        this.setData({
            showCoupon: false
        })
    },
    onInput(e) {
        this.setData({
            note: e.detail.value
        })
    },
    // 提交订单
    toSubmit() {
        let that = this
        let obj = {}
        console.log(this.data.address)
        console.log(this.data.address_id)
        if (this.data.address == '') {
            wx.showToast({
                title: '请选择地址',
                icon: 'error'
            })
            return false
        }
        if (that.data.info != '') {
            obj = {
                mode: 1,
                product_id: this.data.info.product_id,
                product_sku_id: this.data.info.product_sku_id,
                quantity: this.data.info.quantity,
                address_id: this.data.address_id,
                note: this.data.note,
                user_coupon_id:this.data.coupon_id
            }
        }
        if (that.data.mergedArr != '') {
            obj = {
                mode: 2,
                cart_ids: this.data.cart_ids,
                address_id: this.data.address_id,
                note: this.data.note,
                user_coupon_id:this.data.coupon_id
            }
        }
        console.log(obj)
        store.order.submit(obj, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                })
                that.setData({
                    order_id: res.data.order_id,
                    order_no: res.data.order_no,
                })
                that.toPay()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 支付
    toPay() {
        let order_no = this.data.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                store.order.pay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                wx.redirectTo({
                                    url: '/pages/order/index?active=1',
                                })
                            }, 1000)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                wx.redirectTo({
                                    url: '/pages/order/index?active=1',
                                })
                            }, 1000)

                        }
                    })
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let addr = wx.getStorageSync('address')
        if (addr) {
            this.setData({
                address: addr,
                address_id:addr.id
            })
           this.toConOrder()
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})